<?php
    require_once('../includes/DbConnector.php');
    
    if(isset($_GET['action']) && !empty($_GET['action'])) {
        // INPUT DATA.
        $action = $_GET['action'];
        $place = $_GET['place'];
        
        $connector  = new DbConnector();
        $sql = "select
                b.*,
                c.name country,
                ifnull((select bi.image from bar_image bi where b.image_id = bi.image_id), null) image,
                ifnull((select bi.image_type from bar_image bi where b.image_id = bi.image_id), 'image/png') image_type,
                p.name place,
                (select avg(ub.rating) from user_bar ub where ub.bar_id = b.bar_id) rating,
                (select count(1) from user_bar ub where ub.bar_id = b.bar_id) no_of_ratings
                from bar b, place p, country c
                where b.postal_no = p.postal_no
                and p.country_code = c.country_code
                and p.name = '$place'
                and b.status_id = 1
                order by rating desc, no_of_ratings desc, b.name asc";
        $result = $connector->query($sql);
        
        $a=array();
        while($row = mysql_fetch_array($result, SQLITE3_ASSOC)){
            $row['image'] = base64_encode($row['image']);
            array_push($a,$row);
        }
        echo json_encode($a);
    }
?>